<?php
namespace app\job;
use core\HttpUtil;
use core\App;
use core\Util;
use app\model\FilmList;
class DyttJob extends \core\Job{
	function __construct(){
		//初始化一个采集客户端
		$this->httpClient = HttpUtil::getClient();
		$this->domain = "http://www.dygod.net";
	}
	
	//任务运行的入口
	function run(){
		$url = $this->domain."/html/gndy/dyzz/index.html";
		$filmList = $this->getList($url);
		print_r($filmList);
	}
	
	
	//采集列表
	function getList($url){
		$result = $this->httpClient->get($url,['encode'=>'gbk']);  //访问取得返回内容
		$result = iconv("gbk","utf-8",$result);
		$start  = strpos($result,"co_content8");
		$end  = strpos($result,"页次");
		$result = substr($result,$start, $end);
	
		//规则
		//<a href="/html/gndy/dyzz/20151001/106956.html" class="ulink" title="2015年白百何阮经天爱情片《恋爱中的城市》HD国语中字">2015年白百何阮经天爱情片《恋爱中的城市》HD国语中字</a>
		//[\s\S]+?《([\s\S]+?)》([A-Za-z0-9x]+)[\s\S]+?日期：([\s\S]+?)点击：([\s\S]+?)
		$isMath = preg_match_all("/href=\"([^\"]+?)\"[\s\S]+?《([\s\S]+?)》([A-Za-z0-9x]+)[\s\S]+?日期：([\s\S]+?)点击：([\s\S]+?)</",$result,$matchs);

		//解析
		$filmList = [];
		if($isMath){
			for($i=0;$i<count($matchs[1]);$i++){
				$href = trim($this->domain.$matchs[1][$i]);
				$title = trim($matchs[2][$i]);
				$bdType = trim($matchs[3][$i]);
				$date = trim($matchs[4][$i]);
				$clicks = trim($matchs[5][$i]);
				$len = strpos($title,'/');
				if ($len) {
					$title = substr($title,0,$len);
				}
				$dygod = [];
				$dygod['title'] = $title;
				$dygod['seo_key'] = $title."在线播放,".$title."免费播放";
				$dygod['seo_description'] = $title.",".$title."在线播放,免费观看";
				$dygod['ori_url'] = $href;
				$dygod['clarity'] = $bdType;
				$dygod['create_time'] = $date;
				$dygod['hit_nums'] = $clicks;
				$filmList[] = $dygod;
			}
		}
		return $filmList;
	}
}